#version 150

#moj_import <fog.glsl>

uniform sampler2D Sampler0;

uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;
uniform vec4 FogColor;

in float zpos;
in float vertexDistance;
in vec4 vertexColor;
in vec2 texCoord0;
in vec2 texCoord1;
in vec4 normal;

out vec4 fragColor;

in float depth;

bool check_float(float textureAlpha, float targetAlpha) {//用于检测10进制alpha值的函数，由于有误差需要用区间检测，所以专门写了个函数
	
	float targetLess = targetAlpha - 0.01;
	float targetMore = targetAlpha + 0.01;
	return (textureAlpha > targetLess && textureAlpha < targetMore);
	
}

void main() {
    vec4 color = texture(Sampler0, texCoord0) * vertexColor * ColorModulator;
	float alpha = textureLod(Sampler0, texCoord0, 0.0).a * 255.0;
	if (color.a < 0.1) discard;

	if(depth < 0.01 && check_float(alpha, 253.0))discard;//non-gui,discard 2D texture.
	if(depth > 0.01 && (check_float(alpha, 254.0) || check_float(alpha, 252.0)))discard;//in gui,discard 3D and glow texure.

	fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
	if(check_float(alpha,252)) fragColor = texture(Sampler0, texCoord0)*ColorModulator;//这条是自发光检测


}

/*
depth < 0.0 model in world
depth ∈ (0.0,0.01) player dummy
depth ∈ (0.01,0.02) hotbar
depth ∈ (0.02,0.03) gui
*/